package algorithm.recursionAndbacktrack.permuation;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author CandyWall
 * @Date 2022/2/3--13:40
 * @Description 给定一个不含重复数字的数组 nums ，返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
 */
public class PermutationTest {
    private ArrayList<List<Integer>> permutations;
    private boolean[] visited;

    public List<List<Integer>> getPermutations(int[] nums) {
        permutations = new ArrayList<>();
        visited = new boolean[nums.length];
        if (nums != null && nums.length > 0) {
            getPermutations(nums, new ArrayList<Integer>());
        }
        return permutations;
    }

    private void getPermutations(int[] nums, ArrayList<Integer> permuatation) {
        if (permuatation.size() == nums.length) {
            permutations.add(new ArrayList<>(permuatation));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            if (!visited[i]) {
                visited[i] = true;
                permuatation.add(nums[i]);
                getPermutations(nums, permuatation);
                permuatation.remove(permuatation.size() - 1);
                visited[i] = false;
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(new PermutationTest().getPermutations(new int[] {1, 2, 3}));
    }
}
